package com.ruiyi.adrobot.web.controller.fun;/**
 * Created by tao.zeng on 2017/6/8.
 */

import com.alibaba.fastjson.JSONObject;
import com.ruiyi.adrobot.beans.Fun;
import com.ruiyi.adrobot.beans.common.MongoPageRequest;
import com.ruiyi.adrobot.beans.common.ResultMsg;
import com.ruiyi.adrobot.mongo.fun.FunRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 业务类型
 *
 * @author
 * @create 2017-06-08 9:30
 **/
@Controller
@RequestMapping("/fun")
public class FunController {
    @Autowired
    private FunRepository funRepository;

    @RequestMapping(value = "/toList", method = RequestMethod.GET)

    public String toList() {
        return "fun/list";
    }

    @PostMapping("/updateReamrk")
    @ResponseBody
    public ResultMsg<Map<String, Object>> updateRemark(Integer fun, String remark) {
        Fun oldFun = funRepository.findByFun(fun);
        oldFun.setRemark(remark);
        funRepository.save(oldFun);
        return ResultMsg.success();
    }

    @GetMapping("/toAdd")
    public String toAdd() {
        return "fun/add";
    }

    @GetMapping("/getListByPage")
    @ResponseBody
    public Page<Fun> getListByPage(MongoPageRequest<Fun> mongoPageRequest, Fun fun) {
        mongoPageRequest.setParams(fun);
        return funRepository.getListByPage(mongoPageRequest);
    }

    @GetMapping("/findAll")
    @ResponseBody
    public List<Fun> getAll(Fun fun) {
        return funRepository.findAll();
    }

    @GetMapping("/getOne")
    @ResponseBody
    public Fun getOne(Integer fun) {
        return funRepository.findByFun(fun);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg<Map<String, Object>> delete(String id, Boolean onlyDeleteAccount) {
        if (onlyDeleteAccount) {
            funRepository.removeUsedFunsByFun(funRepository.findOne(id).getFun());
            return ResultMsg.success();
        }
        funRepository.removeUsedFunsByFun(funRepository.findOne(id).getFun());
        funRepository.delete(id);
        return ResultMsg.success();
    }

    @GetMapping("/flow/delete")
    @ResponseBody
    public ResultMsg<Map<String, Object>> flowDelete(String id, Integer fun) {
        funRepository.removeFlowByIdAndFun(id, fun);
        return ResultMsg.success();
    }

    @PostMapping("/add")
    @ResponseBody
    public ResultMsg<Map<String, Object>> add(Fun fun) {
        Fun oldFun = funRepository.getByFunOrDescribe(fun.getFun(), fun.getDescribe());
        if (null != oldFun) {
            return ResultMsg.error("该功能已存在");
        }
        funRepository.insert(fun);
        return ResultMsg.success();
    }

    @GetMapping("/conf")
    public String toConf() {
        return "fun/conf";
    }

    @GetMapping("/toFlowList/{fun}")
    public String toFlowList(@PathVariable("fun") Integer fun, ModelMap model) {
        model.put("fun", fun);
        return "fun/flow/list";
    }

    @GetMapping("/flow/getList/{fun}")
    @ResponseBody
    public List<Fun.Flow> getFlowList(@PathVariable("fun") Integer fun) {
        return funRepository.findByFun(fun).getFlows();
    }

    @GetMapping("/flow/toAdd/{fun}")
    public String toFlowAdd(@PathVariable("fun") Integer fun, ModelMap model) {
        model.put("fun", fun);
        return "fun/flow/add";
    }

    @PostMapping("/flow/add/{fun}")
    @ResponseBody
    public ResultMsg<Map<String, Object>> addFlow(@PathVariable("fun") Integer fun, Fun.Flow flow) {
        Fun oldFun = funRepository.getByFunAndFlowsDescribe(fun, flow.getDescribe());
        if (null != oldFun) {
            return ResultMsg.error("该流程已存在");
        }
        flow.setId(String.valueOf(System.currentTimeMillis()));
        funRepository.appendFlow(fun, flow);
        return ResultMsg.success();
    }

    @GetMapping("/action/toAdd/{fun}/{flowId}")
    public String toFlowAdd(@PathVariable("fun") Integer fun, @PathVariable("flowId") String flowId, ModelMap model) {
        model.put("fun", fun);
        model.put("flowId", "" + flowId);
        return "fun/action/add";
    }

    @RequestMapping(value = "/action/add/{fun}/{flowId}", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg<Map<String, Object>> addFlow(@PathVariable("fun") Integer fun, @PathVariable("flowId") String flowId, String jsonAction) {
        Fun.Flow.Action action = JSONObject.parseObject(jsonAction, Fun.Flow.Action.class);
        funRepository.updateAction(fun, flowId, action);
        return ResultMsg.success();
    }

    @GetMapping("/action/get/{fun}/{flowId}")
    @ResponseBody
    public Fun.Flow.Action get(@PathVariable("fun") Integer fun, @PathVariable("flowId") String flowId) {
        return funRepository.findActionByFunAndFlowsId(fun, flowId);
    }


}
